<?php

/*
 * Copyright (C) 2014 Administrator.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301  USA
 */

/**
 * Description of Csv
 *
 * @author Administrator
 */
class Csv extends Controller {

    public function __construct($ControllerName, $Action, $QueryString) {
        parent::__construct($ControllerName, $Action, $QueryString);
        $this->loadModel('Db');
    }

    /**
     * 导出微代理csv数据
     * http://localhost:81/wshop/?/Csv/getCompanyData/month=2014-08
     * @param type $Query
     */
    public function getCompanyData($Query) {
        $res = $this->Db->query("SELECT
	cp.`name`,
	CONCAT(SUM(amount),'元') AS amount,
	CONCAT(SUM(pcount),'件') AS pcount
FROM
	`company_income_record` cr
LEFT JOIN `companys` cp ON cp.uid = cr.com_id
WHERE
	DATE_FORMAT(`date`, '%Y-%m') = '$Query->month' AND cr.is_seted = 1
GROUP BY
	com_id");
        $rs = array(
            array($Query->month . '微代理结算数据'),
            array(' ', ' ', ' '),
            array('代理人', '结算金额', '成交数量')
        );
        $sum = $this->Db->query("SELECT
        ' ',
	CONCAT(SUM(amount),'元') AS amount,
	CONCAT(SUM(pcount),'件') AS pcount
FROM
	`company_income_record` cr
WHERE
	DATE_FORMAT(`date`, '%Y-%m') = '$Query->month' AND cr.is_seted = 1");
        $rsf = array(
            array(' ', ' ', ' '),
            array(' ', '总结算', '总件数')
        );
        $this->exportCsv("$Query->month-微代理结算数据.csv", array_merge($rs, $res, $rsf, $sum));
    }

    /**
     * 下载订单数据
     * @param type $Query
     */
    public function getOrderDatas($Query) {
        $res = $this->Db->query("
            select oa.user_name,oa.tel_number,oa.address,pi.product_name,od.order_amount,odi.product_count,od.order_time from orders od 
LEFT JOIN orders_detail odi on odi.order_id = od.order_id
LEFT JOIN products_info pi on pi.product_id = odi.product_id
LEFT JOIN orders_address oa on oa.order_id = od.order_id
 where `status` = 'received' AND DATE_FORMAT(od.order_time, '%Y-%m') = '$Query->month' ORDER BY od.order_time DESC;");
        $rs = array(
            array(' ', $Query->month, '销售数据'),
            array(' ', ' ', ' '),
            array('顾客', '电话', '地址', '商品', '总额', '数量', '下单日期')
        );
        $sum = $this->Db->query("select '',sum(od.order_amount),SUM(`product_count`) FROM `orders` od
 where `status` = 'received' AND DATE_FORMAT(od.order_time, '%Y-%m') = '$Query->month';");
        $rsf = array(
            array(' ', ' ', ' '),
            array(' ', '总金额', '总件数')
        );
        $this->exportCsv("$Query->month-微代理结算数据.csv", array_merge($rs, $res, $rsf, $sum));
    }

    /**
     * 导出
     * @param type $filename
     * @param type $rs
     */
    private function exportCsv($filename, $rs) {
        header("Content-Type: application/vnd.ms-excel; charset=UTF-8");
        header("Content-Disposition: attachment;filename=$filename ");
        $str = '';
        foreach ($rs as $row) {
            $str_arr = array();
            foreach ($row as $column) {
                $str_arr[] = '"' . str_replace('"', '""', $column) . '"';
            }
            $str.=implode(',', $str_arr) . PHP_EOL;
        }
        echo $str;
    }

}
